/*********************************************************
          File Name:13.58.cpp
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Thu 17 Sep 2015 09:54:46 PM CST
**********************************************************/

#include <iostream>
#include <algorithm>
#include <initializer_list>
#include <vector>

using std::vector;
using std::sort;
using std::initializer_list;

class Foo
{
        private:
                vector<int> v;
        public:
                Foo(){};
                Foo(initializer_list<int> li);
                Foo sorted() &&;
                Foo sorted() const &;
};

Foo::Foo(initializer_list<int> li)
{
        for(auto &x: li)
                v.push_back(x);
}

bool predicate(int &a, int &b)
{
        return a > b;
}

Foo Foo::sorted() &&
{
        std::cout << "call `&&`\n";
        sort(v.begin(), v.end(), predicate);
        return *this;
}

Foo Foo::sorted() const &
{
        std::cout << "call `const &`\n";
        return Foo(*this).sorted();
}

int main()
{
        //Foo().sorted(); // call `&&`

        Foo f(initializer_list<int>{1, 6, 0, 3});

        f.sorted();     // call `const &`
}
